home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / pmake / customs / cctrl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-15  |  4.4 KB  |  165 lines

  1. /*-
  2.  * cctrl.c --
  3.  *    Program to perform various utility functions on a customs network
  4.  *    or a single customs agent. The agent/network can be aborted,
  5.  *    restarted, pinged, have its debugging parameters set or have
  6.  *    an election forced on it.
  7.  *
  8.  * Copyright (c) 1988, 1989 by the Regents of the University of California
  9.  * Copyright (c) 1988, 1989 by Adam de Boor
  10.  * Copyright (c) 1989 by Berkeley Softworks
  11.  *
  12.  * Permission to use, copy, modify, and distribute this
  13.  * software and its documentation for any non-commercial purpose
  14.  * and without fee is hereby granted, provided that the above copyright
  15.  * notice appears in all copies.  The University of California,
  16.  * Berkeley Softworks and Adam de Boor make no representations about
  17.  * the suitability of this software for any purpose.  It is provided
  18.  * "as is" without express or implied warranty.
  19.  */
  20. #ifndef lint
  21. static char *rcsid =
  22. "$Id: cctrl.c,v 1.6 89/11/14 13:45:55 adam Exp $ SPRITE (Berkeley)";
  23. #endif lint
  24.  
  25. #include    "customs.h"
  26. #include    <sys/time.h>
  27. /*#include    <netinet/in.h>*/
  28. #include    <netdb.h>
  29. #include    <strings.h>
  30.  
  31. Boolean
  32. Response(from, len, data)
  33.     struct sockaddr_in    *from;
  34.     int                  len;
  35.     Rpc_Opaque          data;
  36. {
  37.     printf ("%d bytes of data from %s\n", len, InetNtoA(from->sin_addr));
  38.     return (False);
  39. }
  40.  
  41. main(argc, argv)
  42.     int        argc;
  43.     char    **argv;
  44. {
  45.     int                  sock = Rpc_UdpCreate(False, 0);
  46.     struct timeval    timeout;
  47.     struct sockaddr_in    sin;
  48.     Rpc_Proc          proc;
  49.     char              *cp;
  50.     int                  debug;
  51.     struct servent      *sep;
  52.  
  53.     sep = getservbyname ("customs", "udp");
  54.     if (sep == NULL) {
  55.     printf("customs/udp unknown\n");
  56.     exit(1);
  57.     }
  58.     timeout.tv_sec = 2;
  59.     timeout.tv_usec = 0;
  60.     sin.sin_family = AF_INET;
  61.     sin.sin_port = htons(sep->s_port);
  62.     sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  63.     
  64.     cp = index(argv[0], '/');
  65.     if (cp == (char *)NULL) {
  66.     cp = argv[0];
  67.     } else {
  68.     cp += 1;
  69.     }
  70.     if (strcmp (cp, "restart") == 0) {
  71.     proc = (Rpc_Proc)CUSTOMS_RESTART;
  72.     } else if (strcmp (cp, "abort") == 0) {
  73.     proc = (Rpc_Proc)CUSTOMS_ABORT;
  74.     } else {
  75.     proc = (Rpc_Proc)CUSTOMS_PING;
  76.     }
  77.  
  78.     while (--argc > 0) {
  79.     argv++;
  80.     if (strcmp(*argv, "-restart") == 0) {
  81.         proc = (Rpc_Proc)CUSTOMS_RESTART;
  82.     } else if (strcmp (*argv, "-abort") == 0) {
  83.         proc = (Rpc_Proc)CUSTOMS_ABORT;
  84.     } else if (strcmp(*argv, "-ping") == 0) {
  85.         proc = (Rpc_Proc)CUSTOMS_PING;
  86.     } else if (strcmp(*argv, "-all") == 0) {
  87.         sin.sin_addr.s_addr = htonl(INADDR_ANY);
  88.     } else if (strcmp(*argv, "-elect") == 0) {
  89.         proc = (Rpc_Proc)CUSTOMS_ELECT;
  90.     } else if (strcmp(*argv, "-debug") == 0) {
  91.         proc = CUSTOMS_DEBUG;
  92.         
  93.         if (argc == 1) {
  94.         debug = DEBUG_RPC | DEBUG_CUSTOMS;
  95.         } else {
  96.         argc--, argv++;
  97.         debug = 0;
  98.         while (**argv != '\0') {
  99.             if (**argv == 'r') {
  100.             debug |= DEBUG_RPC;
  101.             } else if (**argv == 'c') {
  102.             debug |= DEBUG_CUSTOMS;
  103.             } else if (**argv == 'n') {
  104.             debug = 0;
  105.             }
  106.             (*argv)++;
  107.         }
  108.         }
  109.     } else if (**argv == '-') {
  110.         printf("Unknown switch: %s\n", *argv);
  111.         printf("Usage: %s [-r] [-all] [host]\n", cp);
  112.         exit(1);
  113.     } else {
  114.         struct hostent *he;
  115.  
  116.         he = gethostbyname(*argv);
  117.         if (he == (struct hostent *)NULL) {
  118.         sin.sin_addr.s_addr = inet_addr(*argv);
  119.         } else {
  120.         bcopy (he->h_addr, &sin.sin_addr, he->h_length);
  121.         }
  122.     }
  123.     }
  124.     
  125.     switch ((Customs_Proc)proc) {
  126.     case CUSTOMS_PING:
  127.         printf ("Pinging %s\n", InetNtoA(sin.sin_addr));
  128.         break;
  129.     case CUSTOMS_ELECT:
  130.         printf ("Forcing election on %s\n", InetNtoA(sin.sin_addr));
  131.         break;
  132.     case CUSTOMS_ABORT:
  133.         printf ("Abort %s? [ny](n) ", InetNtoA(sin.sin_addr));
  134.         if (getchar() != 'y') {
  135.         exit(0);
  136.         }
  137.         break;
  138.     case CUSTOMS_RESTART:
  139.         printf ("Restart %s? [yn](y) ", InetNtoA(sin.sin_addr));
  140.         if (getchar() == 'n') {
  141.         exit(0);
  142.         }
  143.         break;
  144.     case CUSTOMS_DEBUG:
  145.         printf("Setting debug for %s to ", InetNtoA(sin.sin_addr));
  146.         if (debug == (DEBUG_CUSTOMS|DEBUG_RPC)) {
  147.         printf("rpc & customs\n");
  148.         } else if (debug == DEBUG_CUSTOMS) {
  149.         printf("customs only\n");
  150.         } else if (debug == DEBUG_RPC) {
  151.         printf("rpc only\n");
  152.         } else {
  153.         printf("nothing\n");
  154.         }
  155.         (void) Rpc_Broadcast(sock, &sin, proc, sizeof(debug),
  156.                  (Rpc_Opaque)&debug, 0, (Rpc_Opaque)0,
  157.                  2, &timeout, Response);
  158.         exit(0);
  159.         break;
  160.     }
  161.     (void) Rpc_Broadcast (sock, &sin, proc, 0, (Rpc_Opaque)0,
  162.               0, (Rpc_Opaque)0,
  163.               2, &timeout, Response);
  164. }
  165.